//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}

//车辆颜色字典
function carColorFormate(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.carColorObject.carColor;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}

//查看详情
function userInfoDetail(id){
    $('#detailModal').modal({
        keyboard: true
    });
    vm.title = "查看详情";
    vm.getInfo(id);

    console.log("vm.userRegister.idScanUrl----"+vm.userRegister.idScanUrl)
}


$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'recorderReport/userregister/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true, hidden: true },
			{ label: '姓名', name: 'userName', index: 'user_name', width: 80 },
			{ label: '身份证号', name: 'idNumber', index: 'id_number', width: 80 },
			{ label: '手机号', name: 'phone', index: 'phone', width: 80 },
			{ label: '所属职业', name: 'vocation', index: 'vocation', width: 80 },
			{ label: '车牌号', name: 'plateNumber', index: 'plate_number', width: 80 },
			{ label: 'IMIE号', name: 'imei', index: 'imei', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,
					formatter: function (cellValue,options,rowObject) {
						return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellValue));
                    }
			},
            { label: '详情', name: '', index: '',width: 50,
                formatter: function (cellvalue, option, rowObject) {
                    return '&nbsp;&nbsp;<button class="btn btn-info btn-xs" type="button" onclick=userInfoDetail(\"' + rowObject.id + '\")>详情</button>';

                }
            },
			{ label: '车辆识别代码', name: 'vin', index: 'vin', width: 80, hidden: true },
			{ label: '银行卡号', name: 'bankCard', index: 'bank_card', width: 80 , hidden: true},
			{ label: '行车记录仪型号', name: 'model', index: 'model', width: 80 , hidden: true},
			{ label: '行车记录仪编号', name: 'reportNumber', index: 'report_number', width: 80 , hidden: true},
			{ label: '所属银行', name: 'bankName', index: 'bank_name', width: 80 , hidden: true},
			{ label: '车辆颜色', name: 'vehicleColor', index: 'vehicle_color', width: 80 , hidden: true},
			{ label: '身份证扫描件', name: 'idScanUrl', index: 'id_scan_url', width: 80 , hidden: true},
			{ label: '银行卡扫描件', name: 'bankScanUrl', index: 'bank_scan_url', width: 80 , hidden: true},
			{ label: '其他资料', name: 'otherDocument', index: 'other_document', width: 80 , hidden: true},
			{ label: '经度 由行车记录仪上传', name: 'lon', index: 'lon', width: 80 , hidden: true},
			{ label: '纬度 由行车记录仪上传', name: 'lat', index: 'lat', width: 80 , hidden: true},
			{ label: '经纬度更新时间', name: 'uploadTime', index: 'upload_time', width: 80 , hidden: true}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            userName:'',
            idNumber:'',
            bankCard:'',
            phone:'',
            plateNumber:''
		},
		showList: true,
		title: null,
		userRegister: {},
		carColorObject:{}, //车辆颜色集合
		carColor:''
	},
    mounted:function(){
        this.$nextTick(function(){ this.initSysDict()});
    },
	methods: {
        initSysDict:function() {
            $.get(baseURL + "sys/dict/select/carColor", function (r) {
                if (r.code === 0) {
                    vm.carColorObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
        },

        downloadDoc:function(){

        },
		query: function () {
			vm.reload();
		},
		reset: function(){
			vm.q.userName = '';
			vm.q.idNumber = '';
			vm.q.bankCard = '';
			vm.q.phone = '';
			vm.q.plateNumber = '';
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.userRegister = {};
			vm.userRegister.vehicleColor = ''
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.userRegister.id == null ? "recorderReport/userregister/save" : "recorderReport/userregister/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.userRegister),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "recorderReport/userregister/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "recorderReport/userregister/info/"+id, function(r){
                vm.userRegister = r.userRegister;

                $("#idCard_scan").attr("src",vm.userRegister.idScanUrl);
                $("#bankCard_scan").attr("src",vm.userRegister.bankScanUrl);

                // document.getElementById('idCard_scan').src = vm.userRegister.idScanUrl;



            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:
                    {
                    	'userName': vm.q.userName,
                        'idNumber': vm.q.idNumber,
                        'bankCard': vm.q.bankCard,
                        'phone': vm.q.phone,
                        'plateNumber': vm.q.plateNumber
                    },
                page:page
            }).trigger("reloadGrid");
		}
	}
});